import { createLogger, format, transports } from "winston";
import "winston-daily-rotate-file";
import { Paths } from "./paths";

const customFormat = format.combine(
  format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
  format.align(),
  format.printf(i => {
    i.message = i.message.trim();
    return JSON.stringify(i);
  })
);
const defaultOptions = {
  format: customFormat,
  datePattern: "YYYY-MM-DD",
  zippedArchive: false,
  maxSize: "20m",
  maxFiles: "30d"
};

const transportList: any[] = [
  new transports.DailyRotateFile({
    filename: Paths.resources("logs/%DATE%.log"),
    ...defaultOptions
  })
];

const logger = createLogger({
  format: customFormat,
  transports: transportList
});

const Log = {
  info(message: string | Object) {
    logger.info(message);
  },
  error(message: string | Object) {
    logger.error(message);
  }
};

export { Log };
